package info.sean.controller;


import info.sean.domain.Account;
import info.sean.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {


    @Autowired
    AccountService accountService;

    /**
     * 查找所有用户
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
     public String findAll(Model model){
        List<Account> accounts = accountService.findAll();
        model.addAttribute("accounts",accounts);
        return "/list.jsp";
     }

    /**
     * 添加用户
     * @param accout
     * @return
     */
//     @RequestMapping("/addAccount")  两种写法
    @PostMapping("/addAccount")
     public String addAccount(Account accout){
         System.out.println("获取的对线"+ accout);
        accountService.addAccount(accout);
        return "redirect:/account/findAll";

     }

     @RequestMapping("/QueryAccount")
     public String QueryAccountId (Integer id,Model model){
         System.out.println("获取的id"+ id );
         System.out.println("获取的model：" + model);
         Account account = accountService.QueryAccountId(id);
         System.out.println(account);
         model.addAttribute("account",account);
         return "/update.jsp";
     }

    /**
     * 修改
     */
    @PostMapping("/UpdateAccount")
    public String modifyAndSave(Account account){
        accountService.UpdateAccount(account);
        return "redirect:/account/findAll";
    }

    @RequestMapping("/deleteAccountId")
public String removeAccount(Integer id){
        accountService.deleteAccount(id);
    return "redirect:/account/findAll";
}
}
